package com.haodf.skeleton.anim

import android.animation.ValueAnimator
import android.graphics.Canvas
import android.graphics.Paint

/**
 * @author xiaxiao
 * @date 2023/5/10.
 * 自定义 loading动画时继承的父类
 */
abstract class LoadingAnimator : ILoadingAnimtor {
    private var isInit = false
    private var canvas: Canvas? = null
    private var anim: ValueAnimator? = null

    override fun onDraw(cs: Canvas) {
        canvas = cs
        if (!isInit) {
            init()
        }
        drawFrame(cs)
    }

    private fun init() {
        isInit = true
        onInit()
        if (anim == null) {
            anim = createAnim()
        }
        startLoadingAnim()
    }

    private fun startLoadingAnim() {
        synchronized(this) {
            if (anim?.isRunning == true) {
                anim?.cancel()
            }
            anim?.start()
        }
    }

    override fun start() {
        startLoadingAnim()
    }

    override fun cancel() {
        anim?.cancel()
    }

    override fun setPaint(p: Paint) {
        //
    }
}